#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "calculator.h"
#include <QRegularExpressionValidator>
using namespace cv;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    this->setWindowTitle("Interpolation");
    interpolation = new Interpolation;
    ui->setupUi(this);
    //------------------Calculator--------------------------
    connect(ui->calculator, SIGNAL(clicked()), this, SLOT(calculator_clicked()));
    //-------------------Params--------------------------
    interpolation->setStep(ui->step_value->currentText().toFloat());
    interpolation->setRadius(ui->radius_value->text().toFloat());
    interpolation->DegSetPoint(ui->begin_deg->text().toFloat(),ui->radius_value->text().toFloat(),0);
    interpolation->DegSetPoint(ui->end_deg->text().toFloat(),ui->radius_value->text().toFloat(),1);

    // only support int
    ui->end_x_value->setValidator(new QIntValidator(-350,350,this));
    ui->end_y_value->setValidator(new QIntValidator(-350,350,this));
    ui->end_x_value->setPlaceholderText(tr("终点X坐标(-350~350)"));
    ui->end_y_value->setPlaceholderText(tr("终点Y坐标(-350~350)"));
    // only support double,实现2个小数位的限制
    QValidator *validator = new QDoubleValidator(0, 350, 2, this);
    ui->radius_value->setValidator(validator);
    // tips
    ui->radius_value->setPlaceholderText(tr("圆弧半径(0~350)"));
    ui->begin_deg->setPlaceholderText(tr("圆弧起始角(0~360)"));
    ui->end_deg->setPlaceholderText(tr("圆弧结束角(0~360)"));
    ui->begin_deg->setValidator(new QIntValidator(0, 360,this));
    ui->end_deg->setValidator(new QIntValidator(0, 360,this));
    connect (ui->step_value,SIGNAL(currentIndexChanged(int)), this, SLOT(step_valueChanged()));
    connect(ui->radius_value, SIGNAL(editingFinished()), this, SLOT(radius_valueChanged()));
    connect (ui->end_x_value,SIGNAL(editingFinished()), this, SLOT(end_valueChanged()));
    connect (ui->end_y_value,SIGNAL(editingFinished()), this, SLOT(end_valueChanged()));
    connect (ui->begin_deg,SIGNAL(editingFinished()), this, SLOT(Begin_Deg_Changed()));
    connect (ui->end_deg,SIGNAL(editingFinished()), this, SLOT(End_Deg_Changed()));
    //---------------------Interpolation-----------------------------------
    connect(ui->line_button, SIGNAL(clicked()), this, SLOT(LineDraw()));
    connect(ui->arc_button, SIGNAL(clicked()), this, SLOT(ArcDraw()));
    connect(ui->arc_anti_button, SIGNAL(clicked()), this, SLOT(AntiArcDraw()));
    connect(ui->clear_button, SIGNAL(clicked()), this, SLOT(clearCapture()));


    connect(ui->exit, SIGNAL(clicked()), this, SLOT(quit()));
}

MainWindow::~MainWindow()
{
    delete interpolation;
    delete calc;
    delete ui;
}




void MainWindow::calculator_clicked()
{
    calc=new Calculator;
    calc->show();
}

void MainWindow::step_valueChanged()
{
    interpolation->setStep(ui->step_value->currentText().toFloat());
}

void MainWindow::radius_valueChanged()
{
    interpolation->setRadius(ui->radius_value->text().toFloat());
    interpolation->DegSetPoint(ui->begin_deg->text().toFloat(),ui->radius_value->text().toFloat(),0);
    interpolation->DegSetPoint(ui->end_deg->text().toFloat(),ui->radius_value->text().toFloat(),1);
}

void MainWindow::end_valueChanged()
{
    interpolation->setEndPt (Point2f(ui->end_x_value->text().toFloat(),ui->end_y_value->text().toFloat()));

}

void MainWindow::Begin_Deg_Changed()
{
    interpolation->DegSetPoint(ui->begin_deg->text().toFloat(),ui->radius_value->text().toFloat(),0);
}

void MainWindow::End_Deg_Changed()
{
    interpolation->DegSetPoint(ui->end_deg->text().toFloat(),ui->radius_value->text().toFloat(),1);
}



void MainWindow::LineDraw()
{
    interpolation->line_inter ();
}

void MainWindow::ArcDraw()
{
    interpolation->arc_inter ();
}

void MainWindow::AntiArcDraw()
{
    interpolation->anti_arc_inter ();
}


void MainWindow::clearCapture()
{
    interpolation->clear();
}


